<template>
    <div style="height: 100%">
        <open-bpm ref="openBpm" v-if="params.defKey" :params="params"></open-bpm>
    </div>
</template>

<script>
import OpenBpm from "@/views/modules/bpm/workbench/OpenBpm.vue";
import RxLoading from "@/views/modules/bpm/workbench/RxLoading.vue";
import BusEvent from "@/views/modules/form/core/formComponent/BusEvent";
import {Util} from "@lowcode/jpaas-common-lib";
export default {
    name: "bpm-component",
    mixins:[BusEvent],
    props:{
        config:{
            type:Object,
            default:()=>{
                return {}
            }
        },
        layoutItem: {
            type: Object,
            default: () => {
                return {}
            }
        },
        instId:{
            type:String,
            default:""
        }
    },
    components: {
        OpenBpm,
        RxLoading
    },
    data() {
        return {
            params:{
                defKey:"",
                action:"start",
                name:"newDoc",
                instId:""
            }
        }
    },
    created() {
        this.init();
    },
    methods: {
        init(){
            if (this.config) {
                //取路由参数
                var params=this.getRouterParams();
                var instId = "";
                //读取配置的参数
                if (this.config.receive.mapping.length > 0) {
                    var mapping = this.config.receive.mapping[0];
                    if (mapping.valueSource == 'param') {
                        if (this.config.receive.type == "url") {
                            if (params[mapping.valueDef]) {
                                instId = params[mapping.valueDef];
                            }
                        } else {
                            if (params[mapping.name]) {
                                instId = params[mapping.name];
                            }
                        }
                    }
                }
                if(!instId){
                    instId=this.instId;
                }
                if(instId){
                    this.params.instId=instId;
                    this.params.name="openDoc";
                }
                if(this.config.bpm){
                    let bpmConfig= Util.toJson(this.config.bpm);
                    this.params.defKey=bpmConfig.value;
                }
            }
        },
        /**
         * 流程事件处理。
         * @param args
         * {
         *  method:""
         *  data:""
         * }
         */
        handPublicReceive(args){
            //处理自定义方法。
            let method=args.method;
            if(method){
                let data=args.data;
                let openBpm=this.$refs.openBpm;
                if(!openBpm){
                    return;
                }
                openBpm.executeMethod(method,data);
            }
        }
    },
    watch: {
        config:{
            handler: function (val, oldVal) {
                this.init();
            },
            deep: true
        }
    }
}
</script>

<style scoped>
</style>